//
//  LZTabBar.m
//  LZBS
//
//  Created by 郝庆 on 16/6/8.
//  Copyright © 2016年 haoqing. All rights reserved.
//

#import "LZTabBar.h"
#import "LZPublishView.h"

@implementation LZTabBar

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        // 设置TabBar的背景图片
        [self setBackgroundImage:[UIImage imageNamed:@"tabbar-light"]];
        
        // 设置TabBar的字体
        UITabBarItem *item = [UITabBarItem appearance];
        NSMutableDictionary *attributesNormal = [NSMutableDictionary dictionary];
        attributesNormal[NSFontAttributeName] = [UIFont systemFontOfSize:14];
        attributesNormal[NSForegroundColorAttributeName] = [UIColor grayColor];
        [item setTitleTextAttributes:attributesNormal forState:UIControlStateNormal];
        NSMutableDictionary *attributesSelected = [NSMutableDictionary dictionary];
        attributesSelected[NSForegroundColorAttributeName] = [UIColor darkGrayColor];
        [item setTitleTextAttributes:attributesNormal forState:UIControlStateSelected];
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    NSInteger index = 0;
    CGFloat y = 0;
    CGFloat w = self.lz_width / 5;
    CGFloat h = self.lz_height;
    
    for (UIView *subView in self.subviews) {
        
        // 过滤掉非UITabBarButton
        if (subView.class != NSClassFromString(@"UITabBarButton")) continue;
        CGFloat x = index * w;
        if (index >= 2) {
            x += w;
        }
        subView.frame = CGRectMake(x, y, w, h);
        index ++;
        
    }
    // 设置中间按钮
    UIButton *publishBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [publishBtn setImage:[UIImage imageNamed:@"tabBar_publish_icon"] forState:UIControlStateNormal];
    [publishBtn setImage:[UIImage imageNamed:@"tabBar_publish_click_icon"] forState:UIControlStateHighlighted];
    [publishBtn addTarget:self action:@selector(publishClick) forControlEvents:UIControlEventTouchUpInside];
    // 设置中间按钮的frame
    publishBtn.frame = CGRectMake(2 * w, y, w, h);
    [self addSubview:publishBtn];
    
}

#pragma mark - 点击发布按钮
- (void)publishClick
{
    [LZPublishView show];
}

@end
